#include "TextConvertWindow.h"

struct _TextConvertWindow
{
        AdwApplicationWindow parent_instance;
        //
        GtkGrid *dot_grid;
};

G_DEFINE_TYPE(TextConvertWindow,
              text_convert_window,
              ADW_TYPE_APPLICATION_WINDOW)
//

static void
on_save_finish(GObject *source_object, GAsyncResult *res, gpointer user_data)
{
        GError      *error        = NULL;
        const gchar *data_to_save = (const gchar *)user_data;
        GFile       *file =
                gtk_file_dialog_save_finish(GTK_FILE_DIALOG(source_object),
                                            res,
                                            &error);

        if (error) {
                g_printerr("保存错误: %s\n", error->message);
                g_error_free(error);
                return;
        }

        if (!file)
                return;

        GFileOutputStream *stream = NULL;

        stream = g_file_create(file,
                               G_FILE_CREATE_REPLACE_DESTINATION,
                               NULL,
                               &error);
        if (error) {
                g_printerr("创建文件失败: %s\n", error->message);
                g_error_free(error);
                g_object_unref(file);
                return;
        }

        g_output_stream_write(G_OUTPUT_STREAM(stream),
                              data_to_save,
                              strlen(data_to_save),
                              NULL,
                              &error);
        if (error) {
                g_printerr("写入失败: %s\n", error->message);
                g_error_free(error);
        } else {
                g_print("文件保存成功\n");
        }

        g_output_stream_close(G_OUTPUT_STREAM(stream), NULL, NULL);
        g_object_unref(stream);
        g_object_unref(file);
}

static char *cs = "test+data";

static void
save_file_cb(TextConvertWindow *self, gpointer data)
{
        GtkFileDialog *file_dialog = gtk_file_dialog_new();

        gtk_file_dialog_set_title(file_dialog, "保存字模文件");
        gtk_file_dialog_set_initial_name(file_dialog, "未命名.txt");

        GtkFileFilter *filter = gtk_file_filter_new();
        gtk_file_filter_add_pattern(filter, "*.txt");
        gtk_file_filter_set_name(filter, "文本文件");
        gtk_file_dialog_set_default_filter(file_dialog, filter);

        gtk_file_dialog_save(file_dialog,
                             GTK_WINDOW(self),
                             NULL,
                             on_save_finish,
                             (gpointer)(cs));
}

static void
text_convert_window_finalize(GObject *object)
{
        G_OBJECT_CLASS(text_convert_window_parent_class)->finalize(object);
}

static void
text_convert_window_class_init(TextConvertWindowClass *klass)
{
        GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);

        gtk_widget_class_set_template_from_resource(
                widget_class,
                "/org/korack/tc/TextConvertWindow.xml");
        gtk_widget_class_bind_template_child(widget_class,
                                             TextConvertWindow,
                                             dot_grid);

        gtk_widget_class_bind_template_callback(widget_class, save_file_cb);

        G_OBJECT_CLASS(klass)->finalize = text_convert_window_finalize;
}

static void
text_convert_window_init(TextConvertWindow *self)
{
        gtk_widget_init_template(GTK_WIDGET(self));

        /* 创建按钮方格 */
        for (int i = 0; i < 16; ++i) {
                for (int j = 0; j < 16; ++j) {
                        GtkWidget *toggle = gtk_toggle_button_new();
                        gtk_grid_attach(self->dot_grid, toggle, i, j, 1, 1);
                }
        }
}
